/*
 * @lc app=leetcode.cn id=482 lang=typescript
 *
 * [482] 密钥格式化
 */

// @lc code=start
function licenseKeyFormatting(s: string, k: number): string {
    let keep: string[][] = [[]];
    for (let i = s.length - 1; i >= 0; i--) {
        const element = s[i];

        const head = keep[0];
        if (element === "-") {
            continue;
        } else {
            const count = head.unshift(element.toUpperCase());
            if (count === k) keep.unshift([]);
        }
    }
    if (keep[0].length === 0) keep.shift();
    return keep.map((i) => i.join("")).join("-");
}
// @lc code=end
